package com.jdy.haoduoaiteacher.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.ColorMatrixColorFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

@SuppressLint("ClickableViewAccessibility")
public class CommonUtil {

  public static boolean hasSDCard() {
    String status = Environment.getExternalStorageState();
    if (!status.equals(Environment.MEDIA_MOUNTED)) {
      return false;
    }
    return true;
  }

  public static String getRootFilePath() {
    if (hasSDCard()) {
      return Environment.getExternalStorageDirectory().getAbsolutePath() + "/";// filePath:/sdcard/
    } else {
      return Environment.getDataDirectory().getAbsolutePath() + "/data/"; // filePath:
                                                                          // /data/data/
    }
  }

  public static boolean checkNetState(Context context) {
    boolean netstate = false;
    ConnectivityManager connectivity = (ConnectivityManager) context
        .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
      NetworkInfo[] info = connectivity.getAllNetworkInfo();
      if (info != null) {
        for (int i = 0; i < info.length; i++) {
          if (info[i].getState() == NetworkInfo.State.CONNECTED) {
            netstate = true;
            break;
          }
        }
      }
    }
    return netstate;
  }

  public static void showToask(Context context, String tip) {
    Toast.makeText(context, tip, Toast.LENGTH_SHORT).show();
  }

  @SuppressWarnings("deprecation")
  public static int getScreenWidth(Context context) {
    WindowManager manager = (WindowManager) context
        .getSystemService(Context.WINDOW_SERVICE);
    Display display = manager.getDefaultDisplay();
    return display.getWidth();
  }

  @SuppressWarnings("deprecation")
  public static int getScreenHeight(Context context) {
    WindowManager manager = (WindowManager) context
        .getSystemService(Context.WINDOW_SERVICE);
    Display display = manager.getDefaultDisplay();
    return display.getHeight();
  }

  /**
   * 让控件点击时，颜色变深
   * */
  public static final OnTouchListener VIEW_TOUCH_DARK = new OnTouchListener() {

    public final float[] BT_SELECTED = new float[] { 1, 0, 0, 0, -50, 0, 1, 0,
        0, -50, 0, 0, 1, 0, -50, 0, 0, 0, 1, 0 };

    @SuppressWarnings("deprecation")
    @Override
    public boolean onTouch(View v, MotionEvent event) {
      if (event.getAction() == MotionEvent.ACTION_DOWN) {
        if (v instanceof ImageView) {
          ImageView iv = (ImageView) v;
          iv.setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));
        } else {
          if (v != null && v.getBackground() != null) {
            v.getBackground().setColorFilter(
                new ColorMatrixColorFilter(BT_SELECTED));
            v.setBackgroundDrawable(v.getBackground());
          }
        }
      } else if (event.getAction() == MotionEvent.ACTION_UP) {
        if (v instanceof ImageView) {
          ImageView iv = (ImageView) v;
          iv.clearColorFilter();
        } else {
          if (v != null && v.getBackground() != null) {
            v.getBackground().clearColorFilter();
            v.setBackgroundDrawable(v.getBackground());
          }
        }
      } else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
        if (v instanceof ImageView) {
          ImageView iv = (ImageView) v;
          iv.clearColorFilter();
        } else {
          if (v != null && v.getBackground() != null) {
            v.getBackground().clearColorFilter();
            v.setBackgroundDrawable(v.getBackground());
          }
        }
      }
      return false;
    }
  };

}
